package com.nc.user.service.impl;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nc.user.config.shiro.AuthFilter;
import com.nc.user.config.shiro.AutoShiro;
import com.nc.user.config.shiro.KickoutSessionControlFilter;
import com.nc.user.config.shiro.SystemLogoutFilter;
import com.nc.user.dao.FrmDataResourcesDao;
import com.nc.user.model.entity.FrmUsers;
import com.nc.user.service.LogService;
import com.nc.user.service.RedisService;
import com.nc.user.service.ShiroService;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@Service
public class ShiroServiceImpl implements ShiroService {
    @Autowired
    ShiroFilterFactoryBean shiroFilterFactoryBean;
    @Autowired
    SecurityManager securityManager;
    @Autowired
    private FrmDataResourcesDao fspDataRecordDao;
    @Autowired
    private LogService logService;
    @Autowired
    private RedisService redisService;
    @Autowired
    CacheManager cacheManager;
    @Autowired
    SessionManager sessionManager;

    @Override
    public Boolean flush() {
        AbstractShiroFilter shiroFilter = null;
        try {
            shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject();
        } catch (Exception e) {

        }
        PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
        DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager();
        // 1. 清空老的权限控制
        manager.getFilterChains().clear();
        shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();

        AutoShiro shiroFilterFactoryBean = new AutoShiro(fspDataRecordDao);
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/home");

        Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
        AuthFilter authFilter = new AuthFilter(logService, redisService);
        authFilter.setLoginUrl("/login");
        authFilter.setUsernameParam("username");
        authFilter.setPasswordParam("password");
        authFilter.setUserClass(FrmUsers.class);
        filters.put("authc", authFilter);
        SystemLogoutFilter logoutFilter = new SystemLogoutFilter(redisService);
        filters.put("loginout", logoutFilter);
        KickoutSessionControlFilter kickoutSessionControlFilter = new KickoutSessionControlFilter();
        kickoutSessionControlFilter.setCacheManager(cacheManager);
        kickoutSessionControlFilter.setSessionManager(sessionManager);
        kickoutSessionControlFilter.setKickoutUrl("login");

        filters.put("kickout", kickoutSessionControlFilter);
        shiroFilterFactoryBean.setFilters(filters);

        Map<String, String> chains = shiroFilterFactoryBean.getFilterChainDefinitionMap();
        for (Map.Entry<String, String> entry : chains.entrySet()) {
            String url = entry.getKey();
            String chainDefinition = entry.getValue().trim().replace(" ", "");
            manager.createChain(url, chainDefinition);
        }
        return true;
    }
}
